package com.lyh.crm.workbench.service.impl;

import com.lyh.crm.settings.dao.UserDao;
import com.lyh.crm.settings.domain.User;
import com.lyh.crm.utils.SqlSessionUtil;
import com.lyh.crm.vo.PaginationVO;
import com.lyh.crm.workbench.dao.ContactsDao;
import com.lyh.crm.workbench.dao.CustomerDao;
import com.lyh.crm.workbench.dao.CustomerRemarkDao;
import com.lyh.crm.workbench.dao.TranDao;
import com.lyh.crm.workbench.domain.*;
import com.lyh.crm.workbench.service.CustomerService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author 北京动力节点
 */
public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);
    private CustomerRemarkDao customerRemarkDao = SqlSessionUtil.getSqlSession().getMapper(CustomerRemarkDao.class);

    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    private TranDao tranDao = SqlSessionUtil.getSqlSession().getMapper(TranDao.class);

    private ContactsDao contactsDao = SqlSessionUtil.getSqlSession().getMapper(ContactsDao.class);

    public List<String> getCustomerName(String name) {

        List<String> sList = customerDao.getCustomerName(name);

        return sList;
    }

    public boolean save(Customer cus) {

        boolean flag = true;

        int count = customerDao.save(cus);
        System.out.println("count = " + count);
        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public PaginationVO<Customer> pageList(Map<String, Object> map) {
        //取得total
        int total = customerDao.getTotalByCondition(map);
        //取得dataList
        List<Customer> dataList = customerDao.getCustomerListByCondition(map);


        for (Customer customer : dataList) {
            String priority = customer.getPriority();
            if(priority==null||"".equals(priority)||"常规".equals(priority)||"null".equals(priority)){
                priority="普通用户";
            }else if ("低".equals(priority)){
                priority="非优先客户";
            }else if ("高".equals(priority)){
                priority="优质客户";
            }else if ("最高".equals(priority)){
                priority="重点客户";
            }
            customer.setPriority(priority);
        }

        //创建一个vo对象，将total和dataList封装到vo中
        PaginationVO<Customer> vo = new PaginationVO<Customer>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回
        return vo;
    }

    public boolean deleteByIds(String[] ids) {

        boolean flag = true;

        //查询出需要删除的备注的数量
        int count1 = customerRemarkDao.getCountByAids(ids);

        //删除备注，返回受到影响的条数（实际删除的数量）
        int count2 = customerRemarkDao.deleteByAids(ids);

        if (count1 != count2) {

            flag = false;

        }

        //查询出需要修改的联系人customerId的数量
        int count3 = customerRemarkDao.getContactsCountByAids(ids);

        //修改联系人customerId，返回受到影响的条数（实际删除的数量）
        int count4 = customerRemarkDao.updateContactsByAids(ids);

        if (count3 != count4) {

            flag = false;

        }

        //删除客户
        int count = customerDao.deleteByIds(ids);
        if (count != ids.length) {

            flag = false;

        }

        return flag;
    }

    public Map<String, Object> getUserListAndCustomer(String id) {

        //取uList
        List<User> uList = userDao.getUserList();

        //取cus
        Customer cus = customerDao.getById(id);
        System.out.println(cus);
        //将uList和cus打包到map中
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("uList", uList);
        map.put("cus", cus);

        //返回map就可以了
        return map;
    }

    public boolean update(Customer cus) {
        boolean flag = true;

        int count = customerDao.update(cus);
        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public Customer detail(String id,String tag,Tran t) {

        Customer cus = customerDao.detail(id);

        if(tag!=null && "1".equals(tag)){
            System.out.println("t = " + t);
            Map<String,String> map = new HashMap<String, String>();
            map.put("contactsId",t.getContactsId());
            map.put("customerId",t.getCustomerId());
            map.put("editBy",t.getEditBy());
            map.put("editTime",t.getEditTime());
            int count = contactsDao.changeCustomerId(map);
            if(count!=0){
                System.out.println("count = " + count);
            }

        }

        return cus;
    }

    public Customer detail(String id) {

        Customer cus = customerDao.detail(id);

        return cus;
    }

    public List<CustomerRemark> getRemarkListByAid(String customerId) {

        List<CustomerRemark> crList = customerRemarkDao.getRemarkListByAid(customerId);

        return crList;
    }

    public boolean deleteRemark(String id) {

        boolean flag = true;

        int count = customerRemarkDao.deleteById(id);

        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public boolean saveRemark(CustomerRemark cr) {

        boolean flag = true;

        int count = customerRemarkDao.saveRemark(cr);

        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public boolean updateRemark(CustomerRemark cr) {

        boolean flag = true;

        int count = customerRemarkDao.updateRemark(cr);

        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public List<Tran> getTranListByAid(Map<String, String> map) {
        List<Tran> tranList = tranDao.getTranListByAid(map);

        return tranList;
    }

    public List<Contacts> getContactsListByCusId(String customerId) {
        List<Contacts> contactsList = contactsDao.getContactsListByCusId(customerId);

        return contactsList;
    }

    public Map<String, Object> getCharts() {

        List<Map<String, Object>> mapList = customerDao.getCharts();
        List<String> dataList = new ArrayList<String>();
        dataList.add("非优先客户");
        dataList.add("普通用户");
        dataList.add("优质客户");
        dataList.add("重点客户");
        int count1=0;
        int count2=0;
        int count3=0;
        int count4=0;
        List<Integer> total = new ArrayList<Integer>();
        for (Map<String, Object> map : mapList) {

            String name = String.valueOf(map.get("name"));
            String value = String.valueOf(map.get("total"));
            String priority = String.valueOf(map.get("priority"));

            if(priority==null||"".equals(priority)||"常规".equals(priority)||priority=="null"){
                count2++;
            }else if ("低".equals(priority)){
                count1++;
            }else if ("高".equals(priority)){
                count3++;
            }else if ("最高".equals(priority)){
                count4++;
            }

        }

        total.add(count1);
        total.add(count2);
        total.add(count3);
        total.add(count4);

        //将total和dataList保存到map中
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", total);
        map.put("dataList", dataList);

        //返回map
        return map;
    }

    public PaginationVO<Customer> money(Map<String, Object> map) {
        System.out.println("进入service");

        //取得total
        int total = customerDao.getTotalMoney(map);
        //取得dataList
        List<Customer> dataList = customerDao.getCustomerMoney(map);

        for (Customer customer : dataList) {
            String priority = customer.getPriority();
            if(priority==null||"".equals(priority)||"常规".equals(priority)){
                priority="普通用户";
            }else if ("低".equals(priority)){
                priority="非优先客户";
            }else if ("高".equals(priority)){
                priority="优质客户";
            }else if ("最高".equals(priority)){
                priority="重点客户";
            }
            customer.setPriority(priority);
        }

        PaginationVO<Customer> vo = new PaginationVO<Customer>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回
        return vo;
    }

    public List<Customer> getCustomerByPriority(String priority) {

        if("普通用户".equals(priority)){
            priority="常规";
        }else if ("非优先客户".equals(priority)){
            priority="低";
        }else if ("优质客户".equals(priority)){
            priority="高";
        }else if ("重点客户".equals(priority)){
            priority="最高";
        }

        List<Map<String, Object>> mapList = customerDao.getCustomerByPriority();
        List<Customer> cusList = new ArrayList<Customer>();
        Customer cus = null;
        for (Map<String, Object> map : mapList) {

            String id = String.valueOf(map.get("id"));
            String name = String.valueOf(map.get("name"));
            String owner = String.valueOf(map.get("owner"));
            String phone = String.valueOf(map.get("phone"));
            String website = String.valueOf(map.get("website"));
            String priority2 = String.valueOf(map.get("priority"));
            System.out.println("priority2 = " + priority2);

            if(priority2==null||"".equals(priority2)||"null".equals(priority2)){
                priority2="常规";
            }

            if(priority.equals(priority2)){
                cus = new Customer();
                cus.setId(id);
                cus.setName(name);
                cus.setOwner(owner);
                cus.setPhone(phone);
                cus.setWebsite(website);
                cus.setPriority(priority2);

                cusList.add(cus);
            }
        }
        return cusList;
    }


}
















